from django.db import models

from gis.common.django_ext.models import BaseModel, ListField
from itpay.core.mch.models import Account as MchAccount


class Account(BaseModel):
    """
    代理账号
    """
    user_name = models.CharField(max_length=50, unique=True)
    nickname = models.CharField(max_length=50, null=True)
    password = models.CharField(max_length=100)
    avatar = models.CharField(max_length=50, null=True)
    use_vcode = models.BooleanField(default=True)  # 是否需要使用安全码
    enable = models.BooleanField(default=False)
    agency_type = models.IntegerField()  # see AgencyType

    balance = models.PositiveIntegerField(default=0)  # 可用额度单位分

    prologue = models.CharField(max_length=1024, null=True)  # 欢迎语
    score = models.FloatField(default=0)  # 分数
    sort_score = models.FloatField(default=2000)  # 排序分数

    mchs = models.ManyToManyField(MchAccount)
    warning_amount = models.IntegerField(default=10000000)
    white_ips = ListField(max_length=1024, null=True)

    working = models.BooleanField(default=False)  # 是否上班

    # im
    im_id = models.BigIntegerField(null=True)
    im_token = models.CharField(max_length=100, null=True)
    im_leaving_time = models.IntegerField(default=600)  # 暂离时间配置单位秒

    # 收款账户自动下发对用户等级要求
    auto_show_level = models.IntegerField(default=0)

    remark = models.CharField(max_length=512, null=True)  # 备注
    parent = models.ForeignKey('self', on_delete=models.PROTECT, blank=True, null=True)


class AccountStat(BaseModel):
    """
    代理数据统计
    """
    account = models.OneToOneField(Account, on_delete=models.CASCADE)
    month_orders_count = models.IntegerField(default=0)  # 月成功订单
    month_good_comments_count = models.IntegerField(default=0)  # 月好评数
    pay_types = ListField(base_type=int, max_length=100, null=True)
    # 收款账户对用户级别要求
    pay_type_1_min_level = models.IntegerField(default=0)
    pay_type_2_min_level = models.IntegerField(default=0)
    pay_type_3_min_level = models.IntegerField(default=0)
    pay_type_4_min_level = models.IntegerField(default=0)
    pay_type_5_min_level = models.IntegerField(default=0)
    auto_arrival_min_level = models.IntegerField(default=0)

    support_auto_arrival = models.BooleanField(default=False)


class AccountScoreDetail(BaseModel):
    """
    代理评分细节
    """
    agency = models.ForeignKey(Account, on_delete=models.CASCADE, db_constraint=False)
    score = models.FloatField()
    after_score = models.FloatField()  # 交易后的余额
    mch = models.ForeignKey(MchAccount, on_delete=models.CASCADE, null=True, db_constraint=False)  # 冗余
    user_id = models.CharField(max_length=50, null=True)  # 冗余
    event_type = models.IntegerField()  # see AgencyScoreEventType
    event_id = models.CharField(max_length=128)  #
    remark = models.CharField(max_length=512, null=True)  # 备注

    class Meta:
        unique_together = ["event_type", "event_id"]


class Token(BaseModel):
    user = models.ForeignKey(Account, on_delete=models.CASCADE)
    token = models.CharField(max_length=64, db_index=True)


class ReceiptConf(BaseModel):
    """
    预置收款方式配置
    """
    name = models.CharField(max_length=100)
    pay_type = models.IntegerField()  # see enum: PayType
    pay_method = models.IntegerField()  # PayMethodType
    receipt_method = models.IntegerField()  # ReceiptMethodType
    auto_arrival = models.BooleanField(default=False)  # 自动上分

    # 定额设置相关
    amount_required_quota = models.BooleanField(default=False)  # 需要设置定额档次
    amount_required_quota_qr_code = models.BooleanField(default=False)  # 需要定额收款二维码
    amount_required_real = models.BooleanField(default=False)  # 需要设置真实到账金额
    amount_allow_custom = models.BooleanField(default=False)  # 是否允许自定义配额

    # im及收银台文案相关配置
    im_amount_tip = models.CharField(max_length=250, null=True)  # im配置：提示选择支付金额文案
    im_pay_tip = models.CharField(max_length=250, null=True)  # im配置：下发收款方式提示文案
    im_pay_success_tip = models.CharField(max_length=250, null=True)  # im配置：支付成功消息文案
    page_top_tip = models.CharField(max_length=250, null=True)  # 收银页顶部文案
    page_bottom_tip = models.CharField(max_length=250, null=True)  # 收银页底部文案

    class Meta:
        unique_together = ('pay_method', 'receipt_method')


class ReceiptAddress(BaseModel):
    """
    代理收款方式
    """
    agency = models.ForeignKey(Account, on_delete=models.PROTECT)
    receipt_conf = models.ForeignKey(ReceiptConf, on_delete=models.PROTECT, null=True)
    pay_types = ListField(base_type=int, max_length=100)  # PayType
    pay_methods = ListField(base_type=int, max_length=100)  # PayMethodType
    receipt_type = models.IntegerField()  # ReceiptType
    receipt_method = models.IntegerField()  # ReceiptMethodType

    account = models.CharField(max_length=100, null=True)  # 账户（支付宝账号，微信账号，银行卡）
    account_qr_code = models.CharField(max_length=100, null=True)  # 个人账号二维码（支付宝，微信）
    nickname = models.CharField(max_length=50, null=True)  # 支付宝、微信个人昵称
    ali_zkl = models.CharField(max_length=100, null=True)  # 支付宝吱口令
    qr_code = models.CharField(max_length=100, null=True)  # 收款二维码
    mch_name = models.CharField(max_length=50, null=True)  # 商户名称
    can_huabei = models.BooleanField(null=True)  # 是否支持支付宝花呗支付
    huabei_fee_rate = models.IntegerField(null=True)  # deprecated in v1.1 花呗收款手续费
    can_credit = models.BooleanField(null=True)  # 是否支持信用卡支付
    credit_fee_rate = models.IntegerField(null=True)  # deprecated in v1.1 信用卡收款手续费
    can_person = models.BooleanField(null=True)  # 是否支持支付宝/微信普往个人账户支付
    first_name = models.CharField(max_length=20, null=True)  # 姓
    last_name = models.CharField(max_length=20, null=True)  # 名
    bank_code = models.CharField(max_length=10, null=True)  # 银行代码 BankCode
    bank_branch = models.CharField(max_length=50, null=True)  # 银行支行
    phone = models.CharField(max_length=20, null=True)  # 银行卡绑定后机号

    amount_required_quota = models.BooleanField(default=False)  # 需要设置定额档次
    amount_required_quota_qr_code = models.BooleanField(default=False)  # 需要定额收款二维码
    amount_required_real = models.BooleanField(default=False)  # 需要设置真实到账金额
    amount_allow_custom = models.BooleanField(default=False)  # 是否允许自定义配额

    status = models.BooleanField(default=False)  # 启用状态
    auto_arrival = models.BooleanField(default=False)  # 自动上分
    auto_show = models.BooleanField(default=True)  # 自动下发
    min_user_level = models.IntegerField(default=0)  # 用户最低等级要求

    min_accumulated_recharge = models.BigIntegerField(default=0)  # 最低累积充值
    max_accumulated_recharge = models.BigIntegerField(null=True)  # 最高累积充值
    daily_amount_limit = models.BigIntegerField(null=True)  # 每日收款金额上限
    daily_amount = models.BigIntegerField(default=0)  # 当日已充值金额
    remark = models.CharField(max_length=300, null=True)  # 备注
    deleted = models.BooleanField(default=False)


class ReceiptAddressQuota(BaseModel):
    """
    收款方式定额
    """
    receipt_address = models.ForeignKey(ReceiptAddress, on_delete=models.CASCADE)
    amount = models.BigIntegerField()
    real_amount = models.BigIntegerField(default=0)
    qr_code = models.CharField(max_length=200, null=True)


class RechargeOrder(BaseModel):
    """
    代理充值
    """
    agency = models.ForeignKey(Account, on_delete=models.PROTECT)
    amount = models.BigIntegerField()
    chase_amount = models.BigIntegerField(default=0)
    before_amount = models.BigIntegerField(default=0)  # 申请时，代理的金额
    status = models.IntegerField()  # see AgencyOrderStatusType
    reason = models.CharField(max_length=300, null=True)
    agency_type = models.IntegerField()  # 冗余，see AgencyType
    success_at = models.DateTimeField(null=True)  # 到账时间


class BalanceTransDetail(BaseModel):
    """
    流水细节
    """
    agency = models.ForeignKey(Account, on_delete=models.PROTECT)
    amount = models.BigIntegerField()
    balance = models.BigIntegerField()  # 交易后的余额
    trans_type = models.IntegerField()  # 交易类型 see AgencyTransactionType
    out_trans_no = models.CharField(max_length=128)  #
    trans_info = models.CharField(max_length=1024, null=True)  # 交易信息描述
    remark = models.CharField(max_length=512, null=True)  # 备注

    class Meta:
        unique_together = ["out_trans_no", "trans_type"]


class UserBlackList(BaseModel):
    """
    黑名单
    """
    agency = models.ForeignKey(Account, on_delete=models.PROTECT)
    mch = models.ForeignKey(MchAccount, on_delete=models.PROTECT)
    user_id = models.CharField(max_length=50)
    remark = models.CharField(max_length=512, null=True)

    class Meta:
        unique_together = ["mch", "user_id", "agency"]


class Complaint(BaseModel):
    agency = models.ForeignKey(Account, on_delete=models.PROTECT, db_constraint=False)
    mch = models.ForeignKey(MchAccount, on_delete=models.PROTECT, db_constraint=False)
    user_id = models.CharField(max_length=64)
    complaint_types = ListField(max_length=512, base_type=int)  # see ComplaintType
    complaint_content = models.CharField(max_length=512, null=True)
    images = ListField(max_length=1024, null=True)
    has_done = models.BooleanField(default=False)  # 是否已处理
    remark = models.CharField(max_length=512, null=True)  # 处理结果
    delete_flag = models.BooleanField(default=False)


class QuickReply(BaseModel):
    agency = models.ForeignKey(Account, on_delete=models.PROTECT, db_constraint=False)
    title = models.CharField(max_length=64)
    content = models.CharField(max_length=1024)


class AgencySortScoreDetail(BaseModel):
    """
    代理排序分数细节
    """
    agency = models.ForeignKey(Account, on_delete=models.PROTECT)
    event_type = models.IntegerField()  # see AgencySortScoreType
    event_id = models.CharField(max_length=128)  #
    mch = models.ForeignKey(MchAccount, on_delete=models.CASCADE, null=True, db_constraint=False)  # 冗余
    user_id = models.CharField(max_length=50, null=True)  # 冗余
    before_score = models.IntegerField()
    score = models.IntegerField()
    trans_info = models.CharField(max_length=1024, null=True)  # 交易信息描述
    remark = models.CharField(max_length=512, null=True)  # 备注

    class Meta:
        unique_together = ["event_id", "event_type"]
